package com.yangwudi.controller;

import com.yangwudi.anno.LogOperation;
import com.yangwudi.pojo.Clazz;
import com.yangwudi.pojo.ClazzQueryParam;
import com.yangwudi.pojo.PageResult;
import com.yangwudi.pojo.Result;
import com.yangwudi.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    /**
     * 查询所有班级信息
     */
    @GetMapping("/list")
    public Result list(){
        log.info("查询全部班级数据");
        List<Clazz> clazzList = clazzService.findAll();
        return Result.success(clazzList);
    }

    /**
     * 班级列表数据的条件分页查询
     */
    @GetMapping
    public Result page(ClazzQueryParam clazzQueryParam){
        log.info("分页查询班级数据:{}",clazzQueryParam);
        PageResult<Clazz> pageResult = clazzService.page(clazzQueryParam);
        return Result.success(pageResult);

    }

    /**
     * 添加班级信息
     */

    @LogOperation
    @PostMapping
    public Result add(@RequestBody Clazz clazz){
        log.info("添加班级数据:{}",clazz);
        clazzService.add(clazz);
        return Result.success();
    }

    /**
     * 根据ID删除班级信息
     */
    @LogOperation
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("根据ID删除班级数据:{}",id);
        clazzService.deleteById(id);
        return Result.success();

    }


    /**
     * 查询回显-根据主键ID查询班级的信息
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("根据ID查询班级信息:{}",id);
        Clazz clazz =clazzService.getById(id);
        return Result.success(clazz);
    }

    /**
     * 修改班级信息
     */
    @LogOperation
    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        log.info("修改班级信息:{}",clazz);
        clazzService.update(clazz);
        return Result.success();
    }
}
